﻿using System;
using System.Net;
using System.Net.Mail;
using System.Text;

namespace CML.Intercamber.Business.Helper
{
    public class MailHelper
    {
        public const string SupportMailAdress = "intercambers@gmail.com";

        public static bool SendMail(string dest, string subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();
                const string fromEmail = "intercambers@gmail.com";
                const string fromPW = "Observatoire51";
                message.From = new MailAddress(fromEmail);
                message.To.Add(dest);

                message.Subject = subject;
                message.Body = body;

                message.BodyEncoding = Encoding.UTF8;
                message.SubjectEncoding = Encoding.UTF8;

                AlternateView htmlView = AlternateView.CreateAlternateViewFromString(
                    @"<table style='width:100%; background: #eee; padding: 20px 10px; font-family: arial;'><tr><td align='center'>
                        <img src='http://intercambers.com/Images/TitreIntercambers.png' style='width: 200px; margin-bottom:25px'/>
                        <div style='background: white; max-width: 800px; padding: 50px 30px; text-align: left;'>"
                     + body.Replace("\n", "<br/>")
                     +@"</div></td></tr></table>"
                    );
                htmlView.ContentType = new System.Net.Mime.ContentType("text/html");
                message.AlternateViews.Add(htmlView);
                
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

                SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587)
                {
                    EnableSsl = true,
                    DeliveryMethod = SmtpDeliveryMethod.Network,
                    UseDefaultCredentials = false,
                    Credentials = new NetworkCredential(fromEmail, fromPW)
                };

                smtpClient.Send(message);
            }
            catch (Exception e)
            {
                e.LogError();
                return false;
            }
            return true;

        }
    }
}